﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cit.CDE.Net.Module.Commons;
using Module;
using Cit.CDE.Net.Module;
using cde.SystemCommon;
using UI.UserControl;
using Cit.CDE.Net.Service.ModuleFactory;
using System.Web.UI.HtmlControls;
using System.Text;

/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        LoreIndex.aspx.cs           
/// Author :         Wangbc
/// CDT:             2013-02-01           
/// Version:         1.0.0.0                
/// Depiction:       课件首页  
/// </summary>
public partial class main_admin_LoreIndex : UserControl.CommonPage
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //查询
            queryLore();

            //查询最新个人通知
            CDEEntityManager msgEm = CDEEMFactory.getEMByFullName("WEB.MSGP_MANAGE");
            CDESearchCriteria msgSc = new CDESearchCriteria(msgEm);
            msgSc.addCondition("DEL_FLG", "0");
            msgSc.addCondition("RECEIVE_USER", GetLoginOprator().SysID);
            msgSc.PageSize = 6;
            msgSc.addSortOrder("INSERT_DATE", "DESC");
            msgSc.addSortOrder("MSG_ID", "DESC");
            
            List<CDEEntity> msgList = CDEService.LoadEntities(msgEm, msgSc);
            StringBuilder msgStr = new StringBuilder();
            foreach (CDEEntity entity in msgList)
            {
                string MSG_TITLE = Util.objectToString(entity.get("MSG_TITLE"));
                string INSERT_DATE = Util.objectToString(entity.get("INSERT_DATE"));
                string msg_id = Util.objectToString(entity.get("MSG_ID"));
                string state = Util.objectToString(entity.get("STATE"));//0:未读,1:已读
                string strRed = "";
                if (state == "0") {
                    strRed = "style=\"color:Red\"";
                }

                msgStr.AppendFormat("<LI><a {0} href=\"javascript:openwin('../admin/MsgPView.aspx?chkID={1}&viewType=1')\" title=\"{2}\">{2}</a></LI>", strRed, msg_id, MSG_TITLE);
            }
            msgListDiv.InnerHtml = msgStr.ToString();

            //查询系统通知
            CDEEntityManager sysmsgEm = CDEEMFactory.getEMByFullName("WEB.MSG_MANAGE");
            CDESearchCriteria sysmsgSc = new CDESearchCriteria(sysmsgEm);
            sysmsgSc.addCondition("DEL_FLG", "0");
            sysmsgSc.PageSize = 6;
            sysmsgSc.addSortOrder("INSERT_DATE", "DESC");
            sysmsgSc.addSortOrder("MSG_ID", "DESC");
            List<CDEEntity> sysmsgList = CDEService.LoadEntities(sysmsgEm, sysmsgSc);
            StringBuilder sysmsgStr = new StringBuilder();
            foreach (CDEEntity entity in sysmsgList)
            {
                string MSG_TITLE = Util.objectToString(entity.get("MSG_TITLE"));
                string INSERT_DATE = Util.objectToString(entity.get("INSERT_DATE"));
                string msg_id = Util.objectToString(entity.get("MSG_ID"));
                sysmsgStr.AppendFormat("<LI><a href=\"javascript:openwin('../admin/MsgView.aspx?chkID={0}&viewType=1')\" title=\"{1}\">{1}</a></LI>", msg_id, MSG_TITLE);
            }


            sysmsgListDiv.InnerHtml = sysmsgStr.ToString();
        }
    }

    /// <summary>
    /// /知识库查询
    /// </summary>
    public void queryLore() {
        string loreTitle = this.txt_LORE_TITLE.Value;
        //类型代码
        string type_code = "";
        //一级分类
        string typecode1 = cutCodeFromCodeAndName(this.cddLoreType1.SelectedValue.ToString());
        //二级分类
        string typecode2 = cutCodeFromCodeAndName(this.cddLoreType2.SelectedValue.ToString());
        //三级分类
        string typecode3 = cutCodeFromCodeAndName(this.cddLoreType3.SelectedValue.ToString());

        if (typecode3 != "")
        {
            type_code = typecode3;
        }
        else if (typecode2 != "")
        {
            type_code = typecode2.Substring(0, 4);
        }
        else if (typecode1 != "")
        {
            type_code = typecode1.Substring(0, 2);
        }


        //知识库
        CDEEntityManager sysmsgEm = CDEEMFactory.getEMByFullName("WEB.LORE_MANAGE");
        CDESearchCriteria sysmsgSc = new CDESearchCriteria(sysmsgEm);
        sysmsgSc.addCondition("DEL_FLG", 0);
        if (type_code != "")
        {
            sysmsgSc.addCondition("LORE_TYPE__TYPE_CODE", type_code, CDEConstCompare.LikeLeft);
        }
        if (loreTitle != "" && loreTitle != "关键字查询")
        {
            sysmsgSc.addCondition("LORE_TITLE", loreTitle, CDEConstCompare.Like);
        }

        //当前登陆人 数据权限处理
        LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];
        string sql = SqlQueryEntity.getUserLoreSql(op);
        if (sql.ToString() != "")
        {
            sysmsgSc.addCondition(" " + sql.ToString());
        }

        sysmsgSc.PageSize = 24;
        sysmsgSc.addSortOrder("LORE_ID", "DESC");

        List<CDEEntity> sysmsgList = CDEService.LoadEntities(sysmsgEm, sysmsgSc);
        StringBuilder sysmsgStr = new StringBuilder();
        foreach (CDEEntity entity in sysmsgList)
        {
            string title = Util.objectToString(entity.get("LORE_TITLE"));
            string FILE_TYPE = Util.objectToString(entity.get("FILE_TYPE"));
            string ID = Util.objectToString(entity.get("LORE_ID"));
            string fileImage = Util.getImageByFileType(FILE_TYPE.ToLower());
            sysmsgStr.AppendFormat("<ul class=\"geshi\">");
            sysmsgStr.AppendFormat("<li class=\"geshi_1\"><a href=\"DownloadLore.aspx?LORE_ID={0}\"><img src=\"{1}\" /></a></li>", ID, fileImage);
            sysmsgStr.AppendFormat("<li class=\"geshi_2\"><a href=\"DownloadLore.aspx?LORE_ID={0}\">{1}</a></li>", ID, title);
            sysmsgStr.AppendFormat("</ul>");
        }

        sysmsgStr.Append("<div style=\"clear:both;\"></div>");
        courseListDiv.InnerHtml = sysmsgStr.ToString();     
    }

    /// <summary>
    /// 返回按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnquery_Click(object sender, EventArgs e)
    {
        //查询
        queryLore();

    }


    /// <summary>
    /// 把联动下拉单中的选择值处理一下只剩下code
    /// </summary>
    /// <param name="codeAndName">处理前</param>
    /// <returns>处理后</returns>
    protected string cutCodeFromCodeAndName(string codeAndName)
    {
        string result = "";
        int index = codeAndName.IndexOf(":");
        if (index > 0)
        {
            result = codeAndName.Substring(0, index);
        }
        return result;
    }     

}
